var RELAY_STATUS_OFF = 0x00 ;
var RELAY_STATUS_ON = 0x01 ;
var RELAY_STATUS_AIR = 0x02 ;
var RELAY_STATUS_COOLING = 0x03 ;
var RELAY_STATUS_HEATING = 0x04 ;
var RELAY_STATUS_PENDING = 0x20 ;
var RELAY_STATUS_UNKNOWN = 0x40 ;
var RELAY_STATUS_NOTUSED = 0x80 ;

var RELAY_STATUS = (function() {
     var private = {
         0x00: 'Off',
         0x01: 'On',
         0x02: 'Air',
         0x03: 'Cool',
         0x04: 'Heat',
         0x20: 'Pend',
         0x40: 'Unknown',
         0x80: 'NotUsed'
     };

     return {
        get: function(name) { 
            if (private[name] == undefined) return 'Unknown'; return private[name]; }
    };
})();

/* Make Layout from $layout packet
$layout:<cx>,<cy>,<row>,<column>,[<cid>,<bid>,<rid>,<name>,<notUsed>,<pos>,[xycxcy 4 items],<rtype>,<status>,<btype>,],[],$

rtype : cuberemote's mRType. always 0 for relay

bid 10, relay 0~2 example
$layout:800,400,1,3,
$layout:800,400,2,5,
    [0,10,0,Relay_10_0,0,0,0,0,0,0,0,  0,0,0,],
    [0,10,1,Relay_10_1,0,1,0,0,0,0,0,  1,1427702792,7652,],$
*/
function GetLayoutFromPacket(msg)
{
    var Layout = {};
    var string_valid=true ;

    // parse root elements
    var regexp_layout_root = /(\$layout:)(\d+),(\d+),(\d+),(\d+),(.*)\$/ ;
    var result = regexp_layout_root.exec(msg); 
    
    if( null==result )
    {
        console.log('GetLayoutFromPacket invalid packet '+msg);
        string_valid = false ;
    }
    
    if( string_valid )
    {
        /*
        console.log('length '+result.length) ;
        for( i=0 ; i<result.length ; i++ )
            console.log(i+' '+result[i]) ;
        */

        var idx=2 ; // skip [0] and [1] $layout
        Layout.cx = result[idx++] ;
        Layout.cy = result[idx++] ;
        Layout.row = result[idx++] ;
        Layout.column = result[idx++] ;
        Layout.Relays = new Array();

        // parse relay list. src  [v,v,],[],
    	var resultr = result[idx].split('],'); 
    	if( resultr.length>0 && ""==resultr[resultr.length-1] )
    	    resultr.length-- ;

        //console.log('relay list length '+resultr.length) ;
        for( i=0 ; i<resultr.length ; i++ )
        {
            //console.log(i+' '+resultr[i]) ;
            var regexp_layout_relay = /\[(\d+),(\d+),(\d+),([^,]+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/ ;
            var resultrelay = regexp_layout_relay.exec(resultr[i]); 

            if( null==resultrelay )
            {
                console.log('invalid relay '+resultr[i]+' of full '+msg);
                string_valid = false ;
                break ;
            }

            /*
            console.log(i+' length '+resultrelay.length) ;
            for( j=0 ; j<resultrelay.length ; j++ )
                console.log(i+'/'+j+' '+resultrelay[j]) ;
                */
    
    		var relay = {};
            var idxr=1 ; // skip [0]
            relay.cid = resultrelay[idxr++] ;
            relay.bid = resultrelay[idxr++] ;
            relay.rid = resultrelay[idxr++] ;
            relay.name = resultrelay[idxr++] ;
            relay.notUsed = resultrelay[idxr++] ;
            relay.pos = resultrelay[idxr++] ;
            relay.x = resultrelay[idxr++] ;
            relay.y = resultrelay[idxr++] ;
            relay.cx = resultrelay[idxr++] ;
            relay.cy = resultrelay[idxr++] ;
            relay.rtype = resultrelay[idxr++] ;
            relay.status = resultrelay[idxr++] ;
            relay.stime = resultrelay[idxr++] ;
            relay.etime = resultrelay[idxr++] ;

            Layout.Relays.push(relay);
        }
    }
    
    if( !string_valid )
        Layout = null ;
    
    return Layout ;
}

/* Make Update from $update packet
$update:[0,10,0,0,],$
$update:[0,10,2,1,1427702792,8462,],$
*/
function GetUpdateFromPacket(msg)
{
    var Update = {};
    var string_valid=true ;

    // parse update packet
    var regexp_update = /(\$update:)\[(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),\],\$/ ;
    var result = regexp_update.exec(msg); 
    
    if( null==result )
    {
        console.log('GetUpdateFromPacket invalid packet '+msg);
        string_valid = false ;
    }
    
    if( string_valid )
    {
        var idx=2 ; // skip [0] and [1] $update
        Update.cid = result[idx++] ;
        Update.bid = result[idx++] ;
        Update.rid = result[idx++] ;
        Update.status = result[idx++] ;
        Update.stime = result[idx++] ;
        Update.etime = result[idx++] ;
    }

    if( !string_valid )
        Update = null ;
    
    return Update ;
}
